﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LDCRNewEdit.Utils
{
    class CharSetUtil
    {
        private static short[,] gbk_unicode = new short[128, 256];
        private static short[,] gbk_big5 = new short[128, 256];
        private static short[,] big5_gbk = new short[128, 256];
        private static short[,] big5_unicode = new short[128, 256];


        public static void Init()
        {
            try
            {
                string datFile = Resources.GetPath("HZMB");

                FileStream fs = new FileStream(datFile, FileMode.Open);
                byte[] tmp = new byte[2];

                for (int i = 0x81; i <= 0xfe; i++)
                {
                    for (int j = 0x40; j <= 0xfe; j++)
                    {
                        if (j != 0x7f)
                        {
                            fs.Read(tmp, 0, 2);
                            gbk_unicode[i - 128, j] = Tools.BytesToShort(tmp[0], tmp[1]);

                            fs.Read(tmp, 0, 2);
                            gbk_big5[i - 128, j] = Tools.BytesToShort(tmp[0], tmp[1]);
                        }
                    }
                }

                for (int i = 0xa0; i <= 0xfe; i++)
                {
                    for (int j = 0x40; j <= 0xfe; j++)
                    {
                        if (j <= 0x7e || j >= 0xa1)
                        {
                            fs.Read(tmp, 0, 2);
                            big5_unicode[i - 128, j] = Tools.BytesToShort(tmp[0], tmp[1]);

                            fs.Read(tmp, 0, 2);
                            big5_gbk[i - 128, j] = Tools.BytesToShort(tmp[0], tmp[1]);
                        }
                    }
                }

                fs.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /**
	     * BIG5字符串转为GBK字节
	     * @param str
	     * @return	
	     */
        public static byte[] Big5ToGbk(string str)
        {
            return Encoding.GetEncoding("BIG5").GetBytes(str);
        }

        /**
         * BIG5字节转为GBK字节
         * @param bytes
         * @return
         */
        public static byte[] Big5ToGbk(byte[] bytes)
        {
            if (bytes == null)
            {
                return bytes;
            }
            for (int i = 0; i < bytes.Length; i++)
            {
                if ((bytes[i] & 0xFF) > 127 && i < bytes.Length - 1)
                {
                    int b0 = bytes[i] & 0xFF;
                    int b1 = bytes[i + 1] & 0xFF;
                    byte[] tmp = Tools.ShortToBytes(big5_gbk[b0 - 128, b1]);
                    bytes[i] = tmp[0];
                    bytes[i + 1] = tmp[1];

                    i++;
                }
            }
            return bytes;
        }

        /**
         * BIG5字符串转为GBK字符串
         * @param str
         * @return
         */
        public static string Big5ToGbkStr(string str)
        {
            return Encoding.GetEncoding("GBK").GetString(Big5ToGbk(str));
        }

        /**
         * BIG5字符串转为GBK字符串
         * @param str
         * @return
         */
        public static string Big5ToGbkStr(byte[] bytes)
        {
            return Encoding.GetEncoding("GBK").GetString(Big5ToGbk(bytes)).TrimEnd('\0');
        }

        /**
         * GBK字节转为BIG5字节
         * @param bytes
         * @return
         */
        public static byte[] GbkToBig5(byte[] bytes)
        {
            for (int i = 0; i < bytes.Length; i++)
            {
                if ((bytes[i] & 0xFF) > 127)
                {
                    int b0 = bytes[i] & 0xFF;
                    int b1 = bytes[i + 1] & 0xFF;
                    byte[] tmp = Tools.ShortToBytes(gbk_big5[b0 - 128, b1]);

                    bytes[i] = tmp[0];
                    bytes[i + 1] = tmp[1];

                    i++;
                }
            }

            return bytes;
        }

        public static byte[] GbkToBig5(string str)
        {
            return GbkToBig5(Encoding.GetEncoding("GBK").GetBytes(str));

        }

        public static string GbkToBig5Str(string str)
        {
            return Encoding.GetEncoding("BIG5").GetString(GbkToBig5(str));

        }
        public static String GbkToBig5Str(byte[] bytes)
        {
            return Encoding.GetEncoding("BIG5").GetString(GbkToBig5(bytes));
        }

    }
}
